package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.BoolEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mapper.OrderAddressEntityMapper;
import com.linkhub.oms.dao.mpservice.IOrderAddressService;
import com.linkhub.oms.entity.OrderAddressEntity;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 订单收货地址表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-28
 */
@Service
public class OrderAddressServiceImpl extends ServiceImpl<OrderAddressEntityMapper, OrderAddressEntity> implements IOrderAddressService {

    @Override
    public List<String> listLocalOrderAddressOrderIds(Collection<String> sysOrderIds) {
        if (CollUtil.isEmpty(sysOrderIds)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return QtStreamUtil.map(lambdaQuery()
                        .select(OrderAddressEntity::getSysOrderId)
                        .in(OrderAddressEntity::getSysOrderId, sysOrderIds)
                        .eq(OrderAddressEntity::getUpdateFlag, BoolEnum.YES)
                        .list(),
                OrderAddressEntity::getSysOrderId);
    }

    @Override
    public void deleteBySysOrderIds(Collection<String> sysOrderIds, BoolEnum updateFlag) {
        if (CollUtil.isEmpty(sysOrderIds)) {
            return;
        }

        lambdaUpdate()
                .in(OrderAddressEntity::getSysOrderId, sysOrderIds)
                .eq(Objects.nonNull(updateFlag), OrderAddressEntity::getUpdateFlag, updateFlag)
                .remove();
    }

    @Override
    public void deleteBySysOrderIds(Collection<String> sysOrderIds) {
        this.deleteBySysOrderIds(sysOrderIds, null);
    }

    @Override
    public List<OrderAddressEntity> listBySysOrderIds(List<String> sysOrderIds) {

        if (CollUtil.isEmpty(sysOrderIds)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return lambdaQuery()
                .in(OrderAddressEntity::getSysOrderId, sysOrderIds)
                .list();
    }

    @Override
    public OrderAddressEntity getByOrderIds(String sysOrderIds) {
        return lambdaQuery()
                .eq(OrderAddressEntity::getSysOrderId, sysOrderIds)
                .one();
    }

    @Override
    public List<String> existOrderAddress(Collection<String> sysOrderIds) {
        if (CollUtil.isEmpty(sysOrderIds)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return QtStreamUtil.map(lambdaQuery()
                        .select(OrderAddressEntity::getSysOrderId)
                        .in(OrderAddressEntity::getSysOrderId, sysOrderIds)
                        .list(),
                OrderAddressEntity::getSysOrderId);
    }
}
